package com.scalar.chiptrack.adaptor.amkor;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.users.VendorAdaptor;
import com.scalar.chiptrack.users.dao.AdaptorSetUpDAO;
import com.scalar.ScalarException;
import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.NVPair;
import HTTPClient.CookieModule;

import java.io.BufferedInputStream;
import java.util.Calendar;

/**
 * Created by Chandra Shekar.
 * Date: May 18, 2005
 * Time: 10:20:39 AM
 */

public class AMKORAdaptor extends JobHandler implements AdaptorConstants
{
    private static final String PROTOCOL_SCHEME_HTTPS               = "https";
    private static final String HOST                                = "webdata.amkor.com";
    private static final String POST_URI_LOGIN                      = "/index.cfm";
    private static final String GET_URI_LOGOUT                      = "/index.cfm?event=logout";
    private static final String GET_EVENT_HISTORY_REPORT            = "/index.cfm?event=showApp&appId=1007&clear=1";
    private static final String POST_EVENT_HISTORY_REPORT_DOWNLOAD  = "/index.cfm?event=leh_process";
    private static final String GET_LOT_STATUS_REPORT               = "/index.cfm?event=showApp&appId=1&clear=1";
    private static final String POST_LOT_STATUS_REPORT_DOWNLOAD     = "/index.cfm?event=ls_process";
    private static final int DEFAULT_SSL_PORT = 443;
    private static final int RESPONSE_CODE_ERROR = 400;

    public static final String FILE_NAME_LOT_EVENT_HISTORY  = "AMKOR_LOT_EVENT_HISTORY";
    public static final String FILE_NAME_LOT_STATUS         = "AMKOR_LOT_STATUS";
    public static final String ADAPTOR_NAME                 = "AMKOR_WIP";

    private String m_dataOutputDir      = null; //"c:/chiptrac_akros/adaptor_out/amkor/";
    private Logger m_adaptorLogger      = null;
    private String m_username           = null; //"RKanth";
    private String m_password           = null; //"Ontrac20";
    private String m_accountNumber      = null; //"476";
    private int    m_adaptor_id     = -1;

    public AMKORAdaptor() throws ScalarException
    {           
        try
		{
        	VendorAdaptor vendorAdaptor = AdaptorSetUpDAO.getVendorAdaptorDetailsByVendorNameAndLocation( VENDOR_NAME_AMKOR_ATK, LOCATION_TYPE_ASSEMBLY );

        	if( vendorAdaptor == null )
        	{
                log( "ERROR!! ERROR!! ERROR!! Exception while loading AMKOR Adaptor Username, Password details from database" );
                throw new ScalarException( "Exception while loading AMKOR Adaptor Username, Password details from database" );
        	}

            m_username = vendorAdaptor.getUserId().trim();
            m_password = vendorAdaptor.getPassWord().trim();
            m_accountNumber = vendorAdaptor.getCustomerId().trim();
            m_dataOutputDir = vendorAdaptor.getOutPutDirectory().trim();

            if ( m_dataOutputDir != null )
            {
                if ( m_dataOutputDir.endsWith( "/" ) || m_dataOutputDir.endsWith( "\\" ) )
                {
                    m_dataOutputDir = m_dataOutputDir + m_username;
                }
                else
                {
                    m_dataOutputDir = m_dataOutputDir + "/" + m_username;
                }

                AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
                m_adaptorLogger  =  AdaptorUtils.getAdaptorLogger( m_dataOutputDir, VENDOR_NAME_AMKOR_ATK );
                AdaptorUtils.setLogger( m_adaptorLogger );
            }
        }
        catch ( Exception se )
        {
            log( "ERROR!! ERROR!! ERROR!! Exception while loading AMKOR Adaptor Username, Password details from database, Exception: " + se );
            throw new ScalarException( "Exception while loading AMKOR Adaptor Username, Password details from database" );
        }
    }
    
    
    public static void main( String[] args ) throws Exception
    {
        new AMKORAdaptor().execute();
    }

    public void execute() throws Exception
    {
         m_adaptor_id = AdaptorRunInfo.saveAdaptorLastUpdatedStatus( CONSTANT_ADAPTOR, ADAPTOR_NAME, CONSTANT_SUCCESS, -1);

        //Print the header details
        AdaptorUtils.printHeader(VENDOR_NAME_AMKOR_ATK, CONSTANT_ADAPTOR );

        AdaptorUtils.removeOldFiles( m_dataOutputDir, m_adaptorLogger );

        HTTPConnection connection = login( HOST, DEFAULT_SSL_PORT, m_username, m_password );

        /*
         * This is for Lot Event History Report 
        HTTPResponse rsp = requestLotEventHistoryReport( connection );
        BufferedInputStream bfReader= new BufferedInputStream ( rsp.getInputStream() );
        String lotFile = AdaptorUtils.saveAsBinaryFile( bfReader, m_dataOutputDir, FILE_NAME_LOT_EVENT_HISTORY, AdaptorUtils.FILE_EXTN_XLS, true );
        System.out.println("Done.");
        */
		if ( connection != null )
		{
		
			HTTPResponse rsp = requestLotStatusReport( connection );
			BufferedInputStream bfReader = null;
			
			if( rsp != null )
			{
				bfReader = new BufferedInputStream( rsp.getInputStream()  );
				if( bfReader != null )
				{
					AdaptorUtils.saveAsBinaryFile( bfReader, m_dataOutputDir, FILE_NAME_LOT_STATUS , FILE_EXTN_XLS,true );

                    if ( m_adaptor_id > 0 )
                    {
                        AdaptorRunInfo.saveAdaptorFileInfo( m_adaptor_id , FILE_NAME_LOT_STATUS, FILE_EXTN_XLS, StringUtils.formatDateandTime(DateUtil.getFormattedTimeStamp() )) ;
                    }
				}
				else
				{
					log( "Could not save Lot Status Sheet from Buffer Reader" );
					throw new ScalarException( "Could not save Lot Status Sheet from Buffer Reader" );
				}
            }
			else
			{
				log( "Could not create response object of Lot Status Sheet" );
				throw new ScalarException( "Could not create response object of Lot Status Sheet" );
			}

			System.out.println("Done.");
			logout( connection );
			bfReader = null;
		}
		else
		{
			log( "Could not able to get the Connection to web site" );
			throw new ScalarException( "Could not able to get the Connection to web site" );
		}
    }


    private HTTPConnection login( String host, int port, String username, String password ) throws Exception
    {
        int responseCode    = 0;
        String reasonLine   = null;
        byte[] data         = new byte[1024];

        HTTPConnection conn = new HTTPConnection( PROTOCOL_SCHEME_HTTPS, host, port );
        CookieModule.setCookiePolicyHandler( null );
        HTTPConnection.addDefaultModule( CookieModule.class, -1 );

        NVPair defaultHeaders[] = new NVPair[ 9 ];
        defaultHeaders[ 0 ] = new NVPair( "Accept", "*/*" );
        defaultHeaders[ 1 ] = new NVPair( "Referer", "http://" + host );
        defaultHeaders[ 2 ] = new NVPair( "Accept-Language", "en-us" );
        defaultHeaders[ 3 ] = new NVPair( "Content-Type", "application/x-www-form-urlencoded" );
        defaultHeaders[ 4 ] = new NVPair( "Proxy-Connection", "Keep-Alive" );
        defaultHeaders[ 5 ] = new NVPair( "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" );
        defaultHeaders[ 6 ] = new NVPair( "Host", host );
        defaultHeaders[ 8 ] = new NVPair( "Pragma", "no-cache" );

        conn.setDefaultHeaders( defaultHeaders );
		
        HTTPResponse rsp  = conn.Get( "/" );
		

        while ( rsp.getInputStream().read( data ) != -1 )
        {
           //System.out.println(new String(data));
        }


        NVPair[] formData = new NVPair[ 5 ];

        formData[ 0 ] = new NVPair( "userid", username );
        formData[ 1 ] = new NVPair( "userpw", password );
        formData[ 2 ] = new NVPair( "event", "authenticateuser" );
        formData[ 3 ] = new NVPair( "userAgent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)");
        formData[ 4 ] = new NVPair( "submit", "Login" );

        System.out.print( "Trying to Logging in......" );
        log( "Trying to Logging in......" );

        rsp  = conn.Post( POST_URI_LOGIN, formData );

        responseCode = rsp.getStatusCode();
        reasonLine = rsp.getReasonLine();
        System.out.println( "Response from Server: " + responseCode + " " + reasonLine );
        log( "Response from Server: " + responseCode + " " + reasonLine );

        if ( responseCode >= RESPONSE_CODE_ERROR )
        {
            System.out.println( "Login failed.. response from server: " + responseCode );
            log( "Login failed.. response from server: " + responseCode );
            conn = null;
        }

        /*while ( rsp.getInputStream().read( data ) != -1 )
        {
           //log(new String(data));
        }*/
        System.out.println( "Done." );
        log( "Done." );
        return conn;
    }

    private HTTPResponse requestLotEventHistoryReport( HTTPConnection conn ) throws Exception
    {
        Calendar cal				= Calendar.getInstance();
        java.util.Date todayDate	= cal.getTime();
        String currentDate			= StringUtils.formatDateToString( todayDate, "MM/dd/yyyy" );
        byte[] data					= new byte[1024];

        System.out.println( "\tPassing through the Lot Event Histroy .... " );
        log( "\tPassing through the Lot Event Histroy ..... " );

        HTTPResponse rsp = conn.Get( GET_EVENT_HISTORY_REPORT  );

        /*while ( rsp.getInputStream().read( data ) != -1 )
        {
           //System.out.println(new String(data));
        }*/
        System.out.println( "\tSubmitting HTTP Request for Lot Event Histroy .... " );
        log( "\tSubmitting HTTP Request for Lot Event Histroy ..... " );
        NVPair[] formData = new NVPair[ 66 ];

        formData[ 0 ] = new NVPair( "StartDisplayRow", "1" );
        formData[ 1 ] = new NVPair( "#attributes.fieldName#_filter", "attributes.selectedValueType" );
        formData[ 2 ] = new NVPair( "TheCustomer", "33" );
        formData[ 3 ] = new NVPair( "availableFields", "" );

        formData[ 4 ] = new NVPair( "selectedFields", "LWCSNO" );
        formData[ 5 ] = new NVPair( "selectedFields", "LWCSNM" );
        formData[ 6 ] = new NVPair( "selectedFields", "LWFCID" );
        formData[ 7 ] = new NVPair( "selectedFields", "LWFSDS" );
        formData[ 8 ] = new NVPair( "selectedFields", "LWPLNT" );
        formData[ 9 ] = new NVPair( "selectedFields", "LWBSID" );
        formData[ 10 ] = new NVPair( "selectedFields", "LWCLOT" );
        formData[ 11 ] = new NVPair( "selectedFields", "LWLTID" );
        formData[ 12 ] = new NVPair( "selectedFields", "LWSRDV" );
        formData[ 13 ] = new NVPair( "selectedFields", "LWTGDV" );
        formData[ 14 ] = new NVPair( "selectedFields", "LWSPKD" );
        formData[ 15 ] = new NVPair( "selectedFields", "LWPKGD" );
        formData[ 16 ] = new NVPair( "selectedFields", "LWPKCT" );
        formData[ 17 ] = new NVPair( "selectedFields", "LWDMSN" );
        formData[ 18 ] = new NVPair( "selectedFields", "LWLEAD" );
        formData[ 19 ] = new NVPair( "selectedFields", "LWDBFL" );
        formData[ 20 ] = new NVPair( "selectedFields", "LWWOPR" );
        formData[ 21 ] = new NVPair( "selectedFields", "LWOPDS" );
        formData[ 22 ] = new NVPair( "selectedFields", "LWLHN1" );
        formData[ 23 ] = new NVPair( "selectedFields", "LWLHND" );
        formData[ 24 ] = new NVPair( "selectedFields", "LWTYPE" );
        formData[ 25 ] = new NVPair( "selectedFields", "LWEDAT" );
        formData[ 26 ] = new NVPair( "selectedFields", "LWETIM" );
        formData[ 27 ] = new NVPair( "selectedFields", "LWEVQW" );
        formData[ 28 ] = new NVPair( "selectedFields", "LWEVQD" );
        formData[ 29 ] = new NVPair( "selectedFields", "LWRCDT" );
        formData[ 30 ] = new NVPair( "selectedFields", "LWRCQT" );
        formData[ 31 ] = new NVPair( "selectedFields", "LWSHTO" );
        formData[ 32 ] = new NVPair( "selectedFields", "LWSHP1" );
        formData[ 33 ] = new NVPair( "selectedFields", "LWSHP2" );
        formData[ 34 ] = new NVPair( "selectedFields", "LWSHP3" );
        formData[ 35 ] = new NVPair( "selectedFields", "LWSHP4" );
        formData[ 36 ] = new NVPair( "selectedFields", "LWSHP5" );
        formData[ 37 ] = new NVPair( "selectedFields", "LWSHP6" );
        formData[ 38 ] = new NVPair( "selectedFields", "LWSHP7" );
        formData[ 39 ] = new NVPair( "selectedFields", "LWSHP8" );
        formData[ 40 ] = new NVPair( "selectedFields", "LWSHP9" );
        formData[ 41 ] = new NVPair( "selectedFields", "LWCINV" );
        formData[ 42 ] = new NVPair( "selectedFields", "LWSHNO" );
        formData[ 43 ] = new NVPair( "selectedFields", "LWCPON" );
        formData[ 44 ] = new NVPair( "selectedFields", "LWPINN" );
        formData[ 45 ] = new NVPair( "selectedFields", "LWSIDN" );
        formData[ 46 ] = new NVPair( "selectedFields", "LWMTYP" );
        formData[ 47 ] = new NVPair( "selectedFields", "LWMCST" );
        formData[ 48 ] = new NVPair( "selectedFields", "LWMDES" );
        formData[ 49 ] = new NVPair( "selectedFields", "LWOPSB" );
        formData[ 50 ] = new NVPair( "selectedFields", "LWRPSB" );
        formData[ 51 ] = new NVPair( "selectedFields", "LWOPJS" );
        formData[ 52 ] = new NVPair( "selectedFields", "LWRPJS" );
        formData[ 53 ] = new NVPair( "selectedFields", "LWDTCD" );

        formData[ 54 ] = new NVPair( "Factory", "00" );
        formData[ 55 ] = new NVPair( "EventType", "All" );
        formData[ 56 ] = new NVPair( "Source_Device", "" );
        formData[ 57 ] = new NVPair( "Target_Device", "" );
        formData[ 58 ] = new NVPair( "Package_Description", "All" );
        formData[ 59 ] = new NVPair( "BusinessCode", "All" );
        formData[ 60 ] = new NVPair( "From_Date", currentDate);
        formData[ 61 ] = new NVPair( "To_Date", currentDate);
        formData[ 62 ] = new NVPair( "Cust_PO", "" );
        formData[ 63 ] = new NVPair( "Lot_Number", "" );
        formData[ 64 ] = new NVPair( "ResultFormat", "Excel" );
        formData[ 65 ] = new NVPair( "Submit", "Submit Form" );

        System.out.print( "Downloading Event History Report.... " );
        log( "Downloading Event History Report.... " );

        rsp = conn.Post( POST_EVENT_HISTORY_REPORT_DOWNLOAD, formData );

        return rsp;

    }

    private HTTPResponse requestLotStatusReport( HTTPConnection conn ) throws Exception
    {
        System.out.println( "\tPassing through the Lot Status .... " );
        log( "Passing through the Lot Status ..... " );
        byte[] data 		= new byte[1024];
        HTTPResponse rsp 	= null;

        try
		{
	        rsp 	= conn.Get( GET_LOT_STATUS_REPORT  );
	        System.out.println( "\tSubmitting HTTP Request for Lot Status .... " );
	        log( "\tSubmitting HTTP Request for Lot Status ..... " );

            /*while ( rsp.getInputStream().read( data ) != -1 )
            {
               log(new String(data));
            }
            */
            NVPair[] formData = new NVPair[ 93 ];
	
	        formData[ 0 ] = new NVPair( "StartDisplayRow", "1" );
	        formData[ 1 ] = new NVPair( "TheCustomer_filter", "sbu" );
	        formData[ 2 ] = new NVPair( "TheCustomer", m_accountNumber );
	        formData[ 3 ] = new NVPair( "availableFields", "" );
	
	        formData[ 4 ] = new NVPair( "selectedFields", "P4CUNU" );
	        formData[ 5 ] = new NVPair( "selectedFields", "P4CUST" );
	        formData[ 6 ] = new NVPair( "selectedFields", "P4FACA" );
	        formData[ 7 ] = new NVPair( "selectedFields", "P4FCDS" );
	        formData[ 8 ] = new NVPair( "selectedFields", "P4PLNT" );
	        formData[ 9 ] = new NVPair( "selectedFields", "P4BUS" );
	        formData[ 10 ] = new NVPair( "selectedFields", "P4DMDS" );
	        formData[ 11 ] = new NVPair( "selectedFields", "P4PKDS" );
	        formData[ 12 ] = new NVPair( "selectedFields", "P4PKG" );
	        formData[ 13 ] = new NVPair( "selectedFields", "P4DIM" );
	        formData[ 14 ] = new NVPair( "selectedFields", "P4LEAD" );
	        formData[ 15 ] = new NVPair( "selectedFields", "P4FMY" );
	        formData[ 16 ] = new NVPair( "selectedFields", "P4FMYD" );
	        formData[ 17 ] = new NVPair( "selectedFields", "P4SRDV" );
	        formData[ 18 ] = new NVPair( "selectedFields", "P4TGDV" );
	        formData[ 19 ] = new NVPair( "selectedFields", "P4CSLT" );
	        formData[ 20 ] = new NVPair( "selectedFields", "P4OCSL" );
	        formData[ 21 ] = new NVPair( "selectedFields", "P4LOT" );
	        formData[ 22 ] = new NVPair( "selectedFields", "P4AMLT" );
	        formData[ 23 ] = new NVPair( "selectedFields", "P4WFRN" );
	        formData[ 24 ] = new NVPair( "selectedFields", "P4CSPO" );
	        formData[ 25 ] = new NVPair( "selectedFields", "P4DBFL" );
	        formData[ 26 ] = new NVPair( "selectedFields", "P4CROP" );
	        formData[ 27 ] = new NVPair( "selectedFields", "P4SDES" );
	        formData[ 28 ] = new NVPair( "selectedFields", "P4OPSD" );
	        formData[ 29 ] = new NVPair( "selectedFields", "P4LTHN" );
	        formData[ 30 ] = new NVPair( "selectedFields", "P4LHN1" );
	        formData[ 31 ] = new NVPair( "selectedFields", "P4LTHS" );
	        formData[ 32 ] = new NVPair( "selectedFields", "P4LHLD" );
	        formData[ 33 ] = new NVPair( "selectedFields", "P4HLDS" );
	        formData[ 34 ] = new NVPair( "selectedFields", "P4HLDDT" );
	        formData[ 35 ] = new NVPair( "selectedFields", "P4SHTO" );
	        formData[ 36 ] = new NVPair( "selectedFields", "P4DTCD" );
	        formData[ 37 ] = new NVPair( "selectedFields", "P4TCCD" );
	        formData[ 38 ] = new NVPair( "selectedFields", "P4MISC" );
	        formData[ 39 ] = new NVPair( "selectedFields", "P4INV" );
	        formData[ 40 ] = new NVPair( "selectedFields", "P4CINV" );
	        formData[ 41 ] = new NVPair( "selectedFields", "P4AAD8" );
	        formData[ 42 ] = new NVPair( "selectedFields", "P4RCD8" );
	        formData[ 43 ] = new NVPair( "selectedFields", "P4RLD8" );
	        formData[ 44 ] = new NVPair( "selectedFields", "P4SCD8" );
	        formData[ 45 ] = new NVPair( "selectedFields", "P4AYD8" );
	        formData[ 46 ] = new NVPair( "selectedFields", "P4AODT" );
	        formData[ 47 ] = new NVPair( "selectedFields", "P4AAODT" );
	        formData[ 48 ] = new NVPair( "selectedFields", "P4TSD8" );
	        formData[ 49 ] = new NVPair( "selectedFields", "P4TODT" );
	        formData[ 50 ] = new NVPair( "selectedFields", "P4ATODT" );
	        formData[ 51 ] = new NVPair( "selectedFields", "P4FGD8" );
	        formData[ 52 ] = new NVPair( "selectedFields", "P4SBD8" );
	        formData[ 53 ] = new NVPair( "selectedFields", "P4RPSBDT" );
	        formData[ 54 ] = new NVPair( "selectedFields", "P4OSD8" );
	        formData[ 55 ] = new NVPair( "selectedFields", "P4PSD8" );
	        formData[ 56 ] = new NVPair( "selectedFields", "P4CPS8" );
	        formData[ 57 ] = new NVPair( "selectedFields", "P4ASD8" );
	        formData[ 58 ] = new NVPair( "selectedFields", "P4CPD8" );
	        formData[ 59 ] = new NVPair( "selectedFields", "P4INQA" );
	        formData[ 60 ] = new NVPair( "selectedFields", "P4RCQA" );
	        formData[ 61 ] = new NVPair( "selectedFields", "P4BKQA" );
	        formData[ 62 ] = new NVPair( "selectedFields", "P4SCQA" );
	        formData[ 63 ] = new NVPair( "selectedFields", "P4AYQT" );
	        formData[ 64 ] = new NVPair( "selectedFields", "P4TSQT" );
	        formData[ 65 ] = new NVPair( "selectedFields", "P4CRQA" );
	        formData[ 66 ] = new NVPair( "selectedFields", "P4CPQA" );
	        formData[ 67 ] = new NVPair( "selectedFields", "P4SHQA" );
	        formData[ 68 ] = new NVPair( "selectedFields", "P4CSPDT1" );
	        formData[ 69 ] = new NVPair( "selectedFields", "P4CSPDA1" );
	        formData[ 70 ] = new NVPair( "selectedFields", "P4CSPDT2" );
	        formData[ 71 ] = new NVPair( "selectedFields", "P4CSPDA2" );
	        formData[ 72 ] = new NVPair( "selectedFields", "P4PRC3" );
	        formData[ 73 ] = new NVPair( "selectedFields", "P4ASSY3" );
	        formData[ 74 ] = new NVPair( "selectedFields", "P4TEST3" );
	        formData[ 75 ] = new NVPair( "selectedFields", "P4DSFL" );

	        formData[ 76 ] = new NVPair( "Factory", "All" );
	        formData[ 77 ] = new NVPair( "Location", "All" );
	        formData[ 78 ] = new NVPair( "Package_Family", "All");
	        formData[ 79 ] = new NVPair( "Target_Device", "" );
	        formData[ 80 ] = new NVPair( "Package_Description", "All" );
	        formData[ 81 ] = new NVPair( "Lot_Number", "" );
	        formData[ 82 ] = new NVPair( "BusinessCode", "All" );
	        formData[ 83 ] = new NVPair( "HoldLots", "ONNOTON" );
	        formData[ 84 ] = new NVPair( "MissedSB", "No" );
	        formData[ 85 ] = new NVPair( "ClosedLots", "YES" );
	        formData[ 86 ] = new NVPair( "FastTrack", "All" );
	        formData[ 87 ] = new NVPair( "LotType", "All" );
	        formData[ 88 ] = new NVPair( "PO_Number", "" );
	        formData[ 89 ] = new NVPair( "Source_Device", "" );
	        formData[ 90 ] = new NVPair( "Process_CT", "" );
	        formData[ 91 ] = new NVPair( "ResultFormat", "Excel" );
	        formData[ 92 ] = new NVPair( "Submit", "Submit Form" );
	
	        System.out.print( "Downloading Lot Status Report.... " );
	        log( "Downloading Lot Status Report.... " );

	        rsp = conn.Post( POST_LOT_STATUS_REPORT_DOWNLOAD, formData );
		}
        catch( Exception e )
		{
        	log( "Could not send request for the Lot Status Sheet, Exception : "+ e );
        	e.printStackTrace();
        	throw new ScalarException( "Could not send request for the Lot Status Sheet" );
		}
        return rsp;
    }

    /**
     *  Logs out from the AMKOR Site
     */
    private void logout( HTTPConnection conn ) throws Exception
    {
        try
        {
            byte[] data = new byte[1024];
            System.out.print( "\n Logging out... " );
            log( "\n Logging out... " );
            HTTPResponse rsp = conn.Get( GET_URI_LOGOUT );
            System.out.println( "Successfully logged out from AMKOR Site" );
            log( "Successfully logged out from AMKOR Site" );
            /* while ( rsp.getInputStream().read( data ) != -1 )
            {
                //System.out.println(new String(data));
            }*/
            conn = null;
            rsp = null;
        }
        catch ( Exception ignore )
        {
            System.out.println( "Exception Occuered While Logging Out From AMKOR Site" + ignore);
            throw ignore;
        }
    }

    private void log( String message )
    {
        if ( message == null ) return;

        if ( m_adaptorLogger != null )
        {
            synchronized ( m_adaptorLogger )
            {
                m_adaptorLogger.logDebug( message + LINE_SEPARATOR );
            }
        }
        else
        {
            System.out.println( message );
        }
    }
}

